﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using KINLO.Data.Reporitory;
using KINLO.UI.Main.Hepler;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace KINLO.UI.Main.VM
{
    public class ExportToMesVM:ViewModelBase
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(MainVM));
        WeightReporitory DB = new WeightReporitory();
        public ExportToMesVM()
        {
            FrontTime = DateTime.Now.AddHours(-7);
            AfterTime = DateTime.Now;
        }
        RelayCommand cmdExportFront;
        /// <summary>
        /// 导前称重
        /// </summary>
        public RelayCommand CmdExportFront
        {
            get { return cmdExportFront ?? (cmdExportFront = new RelayCommand(() => DoCmdExportFront())); }
        }
        void DoCmdExportFront()
        {
            try
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "Excel 工作簿(*.xls)|*.xls";
                dlg.FileName = DateTime.Now.ToString("yyyy-MM-dd HH点mm分ss秒 ") + "前称重";
                if (dlg.ShowDialog() == true)
                {
                    var frontWeight = DB.GetFrontWeightExportToExcel(FrontTime,AfterTime);
                    var result = ExcelHelper.SaveToFrontAfterWeight(dlg.FileName, frontWeight, "Front");
                    if (result)
                    {
                        MessageBox.Show("保存成功");
                    }
                }
            }
            catch (IOException ioex)
            {
                MessageBox.Show(ioex.Message);
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        RelayCommand cmdExportAfter;
        /// <summary>
        /// 导后称重
        /// </summary>
        public RelayCommand CmdExportAfter
        {
            get { return cmdExportAfter ?? (cmdExportAfter = new RelayCommand(() => DoCmdExportAfter())); }
        }
        void DoCmdExportAfter()
        {
            try
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "Excel 工作簿(*.xls)|*.xls";
                dlg.FileName = DateTime.Now.ToString("yyyy-MM-dd HH点mm分ss秒 ") + "后称重";
                if (dlg.ShowDialog() == true)
                {
                    var frontWeight = DB.GetAfterWeightExportToExcel(FrontTime, AfterTime);
                    var result = ExcelHelper.SaveToFrontAfterWeight(dlg.FileName, frontWeight, "After");
                    if (result)
                    {
                        MessageBox.Show("保存成功");
                    }
                }
            }
            catch (IOException ioex)
            {
                MessageBox.Show(ioex.Message);
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }
        DateTime frontTime;
        public DateTime FrontTime
        {
            get { return frontTime; }
            set
            {
                frontTime = value;
                RaisePropertyChanged("FrontTime");
            }
        }
        DateTime afterTime;
        public DateTime AfterTime
        {
            get { return afterTime; }
            set
            {
                afterTime = value;
                RaisePropertyChanged("AfterTime");
            }
        }
    }
}
